//
//  ClubExchangeDetailViewController.swift
//  77Club
//
//  Created by Apple on 2024/9/18.
//

import UIKit

class ClubExchangeDetailViewController: ClubBaseViewController {

    private let backButton = c_make_ex(UIButton.self)
        .image("Common_blackBack").end()
    private let titleLabel = c_make_ex(UILabel.self)
        .text("兑换明细").color(.black).font(16.font_semiBold).end()
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .white
        tableView.showsVerticalScrollIndicator = false
        tableView.register(ClubExchangeDetailCell.self)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorStyle = .none
        tableView.rowHeight = 164.0.Scale
        return tableView
    }()
    private let emptyView = ClubEmptyView()
    
    private var pageIndex = 1
    private var sourceData = [ClubMyExchangeListModel]()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        loadData()
        
        view.subviews{
            backButton
            titleLabel
            tableView
            emptyView
        }
        
        backButton.leading(0).top(.statusHeight).width(56).height(44)
        titleLabel.Leading == backButton.Trailing
        titleLabel.CenterY == backButton.CenterY
        tableView.leading(0).trailing(0).top(.navi_height).bottom(.safe_bottom)
        emptyView.leading(0).trailing(0).top(.navi_height + 200).height(200)
        
        emptyView.isHidden = true
        emptyView.titleString = "暂无兑换记录~"
        
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
        
        tableView.refresh_loadMore {
            self.loadMoreData()
        }.refresh_pull {
            self.loadNewData()
        }
    }
    
    private func loadData(){

        Task {
            let res = await network.request(.myExchangeList(page: pageIndex),type: ClubMyExchangeListModel.self,isShowError: false)
            tableView.refresh_end()
            if res.isSuccess, let models = res.models {
                self.sourceData = models
                self.emptyView.isHidden = models.count > 0
                self.tableView.reloadData()
            }else{
                self.sourceData.removeAll()
                self.tableView.reloadData()
                emptyView.isHidden = false
            }
        }
    }
    
    @objc private func loadNewData(){
        pageIndex = 1
        loadData()
    }
    
    @objc private func loadMoreData(){
        pageIndex += 1
        Task {
            let res = await network.request(.myExchangeList(page: pageIndex),type: ClubMyExchangeListModel.self,isShowError: false)
            tableView.refresh_end()
            if res.isSuccess, let models = res.models {
                if models.count > 0 {
                    for mod in models {
                        self.sourceData.append(mod)
                    }
                } else {
                    self.tableView.refresh_end(true)
                }
                self.tableView.reloadData()
            }
        }
    }
}

extension ClubExchangeDetailViewController: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return sourceData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "ClubExchangeDetailCell", for: indexPath) as! ClubExchangeDetailCell
        cell.model = sourceData[indexPath.row]
        return cell
    }
}
